package com.lingnan.wanxiangxinxun.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lingnan.wanxiangxinxun.entity.dto.News_addDTO;
import com.lingnan.wanxiangxinxun.entity.dto.News_service_updateDTO;
import com.lingnan.wanxiangxinxun.entity.dto.query.News_queryDTO;
import com.lingnan.wanxiangxinxun.entity.po.News;
import com.lingnan.wanxiangxinxun.entity.po.User;
import com.lingnan.wanxiangxinxun.entity.vo.common.PageVO;
import com.lingnan.wanxiangxinxun.entity.vo.common.Result;
import com.lingnan.wanxiangxinxun.entity.vo.service.NewsVO;
import com.lingnan.wanxiangxinxun.service.INewsService;
import com.lingnan.wanxiangxinxun.service.IUserService;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 新闻信息管理
 */
@RestController
@RequestMapping("/admin/news")
public class NewsController {

    @Autowired
    private INewsService iNewsService;
    @Autowired
    private IUserService userService;


    //新增
    @PostMapping("/add")
    public Result addNews(@RequestBody @Valid News_addDTO newsAddDTO) {
        News news = new News();
        BeanUtils.copyProperties(newsAddDTO, news);
        news.setCreateTime(LocalDateTime.now());
        try {
            //1、保存新闻
            iNewsService.save(news);
            //2、更新作者作品数量
            UpdateWrapper<User> uw = new UpdateWrapper<>();
            uw.eq("id", news.getAuthorId()).setSql("works_num = works_num + 1");
            userService.update(uw);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }
        return Result.success();
    }


    //删除(单删)
    @DeleteMapping("/delete/{id}")
    public Result deleteOneNews(@PathVariable Integer id) {
        News n = iNewsService.getById(id);
        //1、删除新闻
        boolean res = iNewsService.deleteById(id);
        if (!res) {
            return Result.error("删除失败");
        }
        //2、更新作者作品数量
        UpdateWrapper<User> uw = new UpdateWrapper<>();
        uw.eq("id", n.getAuthorId()).setSql("works_num = works_num - 1");
        return Result.success();
    }


    //信息修改
    @PutMapping("/update")
    public Result updateNews(@RequestBody News_service_updateDTO newsDTO) {
        int res = iNewsService.updateOneNewsById(newsDTO);

        if (res == 0) { return Result.error("修改失败"); }
        if (res != 1){ return Result.error("修改发生错误"); }

        return Result.success();
    }


    //设置头条新闻
    @PostMapping("/setHeadlineNews")
    public Result setHeadlineNews(@RequestParam Integer newsId,@RequestParam Integer checkerId) {
        News news = iNewsService.getById(newsId);
        if (news == null) {
            return Result.error("该新闻不存在");
        }
        return iNewsService.setHeadlineNews(newsId, checkerId);
    }




    //查找--分页查所有(按创建时间倒序排)
    @GetMapping("/getAllInPage")
    public Result getNews(@RequestParam(defaultValue = "1") long current,
                          @RequestParam(defaultValue = "10") long size)
    {
        PageVO<NewsVO> allInPage = iNewsService.getAllInPage(current, size);
        return Result.success(allInPage);
    }


    //查找--根据条件查InCondition
    @PostMapping("/getAllInCondition")
    public Result getNewsInCondition(@RequestBody News_queryDTO newsDTO) {
        PageVO<NewsVO> allInCondition = iNewsService.getInCondition(newsDTO);
        return Result.success(allInCondition);
    }


    //查找--根据id查
    @GetMapping("/getById")
    public Result getNewsById(@RequestParam List<Integer> ids, Integer userId) {
        List<News> newsList = iNewsService.getNewsListById(ids, userId);
        return Result.success(newsList);
    }




    //添加热门搜索
    @PostMapping("/setHotSearchList")
    public Result setHotSearchList(@RequestParam String value,@RequestParam Integer checkerId) {
        return iNewsService.setHotSearchList(value, checkerId);
    }

    //删除热搜榜数据
    @DeleteMapping("/deleteHotSearchValue")
    public Result deleteHotSearchValue(@RequestParam Integer index, @RequestParam Integer checkerId) {
        return iNewsService.deleteHotSearchValue(index, checkerId);
    }

    //更改热门搜索顺序（前端拖拽修改）
    @PostMapping("/updateHotSearchListOrder")
    public Result updateHotSearchListOrder(@RequestBody List<String> valueList, @RequestParam Integer checkerId) {
        return iNewsService.updateHotSearchListOrder(valueList, checkerId);
    }

    //修改热搜榜（修改标题）
    @PostMapping("/updateHotSearchList")
    public Result updateHotSearchList(@RequestParam String value, @RequestParam Integer index, @RequestParam Integer checkerId) {
        return iNewsService.updateHotSearchList(value, index, checkerId);
    }

    //查找--热搜榜数据
    @GetMapping("/getHotSearchList")
    public Result getHotSearchList() {
        List<String> hotSearchList = iNewsService.getHotSearchList();
        return Result.success(hotSearchList);
    }



}
